# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
  include TagsHelper
  
  # Taken from php, converts all \n, \r, \r\n to <br/>
  def nl2br(s)
    s.gsub(/\r\n|\r|\n/, '<br />')
  end
  
  def sort_td_class_helper(param)
    result = 'class="sortup"' if params[:sort] == param
    result = 'class="sortdown"' if params[:sort] == param + "_reverse"
    return result
  end
  
  def sort_link_helper(text, param, options={})
    key = param
    key += "_reverse" if params[:sort] == param
    
    url_action = options.delete(:url_action)
    url_action = 'list' if url_action.nil?
    
    options = {
      :url => {:action => url_action, :params => params.merge({:sort => key, :page => nil})},
      :update => 'sort_table_ajax'
    }
    
    html_options = {
      :title => "Sort by this field",
      :href => url_for(:action => url_action, :params => params.merge({:sort => key, :page => nil}))
    }
    
    link_to_remote(text, options, html_options)
  end
  
  
  def ae_some_html(s)
       # converting newlines
       s.gsub!(/\r\n?/, "\n")

       # escaping HTML to entities
       s = s.to_s.gsub('&', '&amp;').gsub('<', '&lt;').gsub('>', '&gt;')

       # blockquote tag support
       s.gsub!(/\n?&lt;blockquote&gt;\n*(.+?)\n*&lt;\/blockquote&gt;/im, "<blockquote>\\1</blockquote>")

       # other tags: b, i, em, strong, u, ul, li
       %w(b i em strong u ul li).each { |x|
            s.gsub!(Regexp.new('&lt;(' + x + ')&gt;(.+?)&lt;/('+x+')&gt;',
                    Regexp::MULTILINE|Regexp::IGNORECASE), 
                    "<\\1>\\2</\\1>")
           }

       # A tag support
       # href="" attribute auto-adds http://
       s = s.gsub(/&lt;a.+?href\s*=\s*['"](.+?)["'].*?&gt;(.+?)&lt;\/a&gt;/im) { |x|
               '<a href="' + ($1.index('://') ? $1 : 'http://'+$1) + "\">" + $2 + "</a>"
             }

       # replacing newlines to <br> ans <p> tags
       # wrapping text into paragraph
       s = "<p>" + s.gsub(/\n\n+/, "</p>\n\n<p>").
               gsub(/([^\n]\n)(?=[^\n])/, '\1<br />') + "</p>"

      # remove <br> achter li and ul
        s.gsub!("</li>\n<br />", '</li>')
        s.gsub!("<ul>\n<br />", '<ul>')
        s.gsub!("</ul>\n<br />", '</ul>')
        
      # make #int to link to ticket
         s = s.gsub(/\#([0-9]+)/) { |x| link_to('#' +  $1 ,:controller => 'ticket', :action =>'show', :id=> $1) }
       s      
   end
   
   def some_html_explanation()
     
     s = '<div class="explanation">
				<p>The function allows following HTML:</p>
				<ul>
					<li>&lt;a href="URL"&gt; <a href="#">link</a> &lt;/a&gt; (only href attribute)</li>
					<li>&lt;b&gt;<b>bold</b>&lt;/b&gt;</li>
					<li>&lt;blockquote&gt;<blockquote>blockquote</blockquote>&lt;/blockquote&gt;</li>
					<li>&lt;em&gt;<em>emphasis</em>&lt;/em&gt;</li>
					<li>&lt;i&gt;<i>italic</i>&lt;/i&gt;</li>
					<li>&lt;strong&gt;<strong>strong</strong>&lt;/strong&gt;</li>
					<li>&lt;u&gt;<u>underlined</u>&lt;/u&gt;</li>
					<li>&lt;ul&gt;,&lt;li&gt; List object</i>
				</ul>
				<p>Any of unclosed or broken tags will not be converted to HTML.</p>
			</div>'
			
			return s
   end
end
